package cn.hl.pet.service.impl;

import cn.hl.basic.service.IBaiduAiAuditService;
import cn.hl.basic.service.impl.BaseServiceImpl;
import cn.hl.basic.util.AjaxResult;
import cn.hl.basic.util.LoginContext;
import cn.hl.org.domain.Employee;
import cn.hl.org.mapper.EmployeeMapper;
import cn.hl.pet.domain.Pet;
import cn.hl.pet.domain.PetDetail;
import cn.hl.pet.domain.PetOnlineAuditLog;
import cn.hl.pet.mapper.PetDetailMapper;
import cn.hl.pet.mapper.PetMapper;
import cn.hl.pet.mapper.PetOnlineAuditLogMapper;
import cn.hl.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void add(Pet pet) {
        super.add(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.add(petDetail);
        }
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.update(petDetail);
        }
    }

    @Override
    public void delete(Long id) {
        petDetailMapper.deleteByPetId(id);
        super.delete(id);
    }

    /**
     * 上架
     * @param ids
     * @param request
     * @return
     */
    @Override
    @Transactional
    public AjaxResult onsale(List<Long> ids, HttpServletRequest request) {
        for (Long id: ids) {
            // 1.根据id查询pet对象
            Pet pet = petMapper.findById(id);
            // 2.调用百度智能审核，审核宠物名称和图片是否合法
            Boolean textBoo = baiduAiAuditService.textAudit(pet.getName());
            Boolean imgBoo = baiduAiAuditService.imageAudit(pet.getResources());
            // 3.调用工具类获取当前登录人信息，再根据logininfoId查询emlooyee对象
            Long logininfoId = LoginContext.getLogininfo(request).getId();
            Employee employee = employeeMapper.findByLogininfoId(logininfoId);
            // 4.判断宠物名称和图片审核是否都合法
            if (textBoo && imgBoo) {
                // 4.1合法，调用方法修改数据库，状态和上架时间
                Map<String,Object> params = new HashMap<>();
                params.put("id",id);
                params.put("onsaletime",new Date());
                petMapper.onsale(params);
                // 4.2将关于审核的信息添加到审核日志中
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(1);
                log.setAuditId(employee.getId());
                log.setNote("审核成功！");
                petOnlineAuditLogMapper.add(log);
            } else {
                // 4.3不合法，将关于审核的信息添加到审核日志中，并返回错误信息
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(id);
                log.setState(0);
                log.setAuditId(employee.getId());
                log.setNote("审核失败！宠物名称或图片不合法");
                petOnlineAuditLogMapper.add(log);
                return AjaxResult.me().setSuccess(false).setMessage("上架失败！宠物名称或图片不合法");
            }
        }
        return new AjaxResult();
    }

    /**
     * 下架
     * @param ids
     * @param request
     * @return
     */
    @Override
    @Transactional
    public AjaxResult offsale(List<Long> ids, HttpServletRequest request) {
        Map<String,Object> params = new HashMap<>();
        params.put("ids",ids);
        params.put("offsaletime",new Date());
        petMapper.offsale(params);
        return new AjaxResult();
    }
}
